package com.solocredit.finance.utils.event

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/6 10:52
 * @Annotation：发布者
 */
class MullaPublisher {

    private var subscriberList: ArrayList<MullaSubscriber>? = null
    init {
        subscriberList = arrayListOf()
    }

    /**
     * 注册订阅者
     */
    fun register(subscriber: MullaSubscriber){
        var isAdd = true
        subscriberList?.let {
            for (i in 0..< it.size){
                if (it[i] == subscriber){
                    isAdd = false
                    break
                }
            }
            if (isAdd){
                it.add(subscriber)
            }
        }
    }

    /**
     * 发布信息
     */
    fun publish(event: MullaEventData){
        subscriberList?.let { list ->
            list.forEach {
                it.onMullaEvent(event)
            }
        }
    }

    /**
     * 移除订阅者
     */
    fun detach(subscriber: MullaSubscriber){
        subscriberList?.remove(subscriber)
    }
}